package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageBox;
import com.moleq.models.GlobalInfo;
import com.moleq.models.Receipt;
import com.moleq.models.StoreSetting;

public class ReceiptService {
	private DatabaseHelper DBHelper;
	private String errorString;
	private Context mContext;
	
	public ReceiptService(){}
	public ReceiptService(Context context){
//		this.DBHelper = new DatabaseHelper(context);
		mContext = context;
		this.DBHelper = new DatabaseHelper(GlobalInfo.getInstance().ShareContext);
	}
	
	public List<Receipt> FindALLHeader(){
		List<Receipt> obj = new ArrayList<Receipt>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "+ DatabaseSchema.Receipt.SCHEMA_NAME +" where rm_type = 'H' order by rm_id", null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		String businessName = "";
		List<StoreSetting> listStore = new ArrayList<StoreSetting>();
		listStore = ServiceManager.getInstance().mStoreService.getStoreSetting();
		if (listStore.size() > 0) {
			businessName = listStore.get(0).getSs_business_name();
			if (businessName != null && !businessName.equals("") && !checkBusinessInHeader()) {
				Receipt header = new Receipt();
				header.setId(0);
				header.setEdit(false);
				header.setMsg(businessName);
				header.setType("H");
				header.setLarge("N");
				header.setJustify("C");
				this.insertReceipt(header);
			}
		}
		while (cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.Receipt.RM_ID));
			String type = cursor.getString(cursor.getColumnIndex(DatabaseSchema.Receipt.RM_TYPE));
			String msg = cursor.getString(cursor.getColumnIndex(DatabaseSchema.Receipt.RM_MSG));
			String create_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.Receipt.CREATE_TIME));
			String update_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.Receipt.UPDATE_TIME));
			String large = cursor.getString(cursor.getColumnIndex(DatabaseSchema.Receipt.RM_LARGE));
			String justify = cursor.getString(cursor.getColumnIndex(DatabaseSchema.Receipt.RM_JUSTIFY));
			
			if (large == null) large = "N";
			if (justify == null) justify = "L";
			//large(size) -- 1.large(L) 2.normal(N)
			//justify -- 1.Left(L) 2.Center(C) 3.Right(R) 
			obj.add(new Receipt(id, type, msg, large, justify, create_datetime, update_datetime));
		}
		cursor.close();
		return obj;
	}
	
	public boolean checkBusinessInHeader(){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "select rm_id from "+ DatabaseSchema.Receipt.SCHEMA_NAME +" where rm_id = 0 and rm_type = 'H'";
		boolean re = false;
		Cursor cursor = db.rawQuery(sql, null);
		
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return re;
		}
		if (cursor.moveToNext()) {
			re = true;
		}
		cursor.close();
		return re;
	}
	
	public List<Receipt> FindALLTrailer(){
		List<Receipt> obj = new ArrayList<Receipt>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery("select * from "+ DatabaseSchema.Receipt.SCHEMA_NAME +" where rm_type = 'T' order by rm_id", null);
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex(DatabaseSchema.Receipt.RM_ID));
			String type = cursor.getString(cursor.getColumnIndex(DatabaseSchema.Receipt.RM_TYPE));
			String msg = cursor.getString(cursor.getColumnIndex(DatabaseSchema.Receipt.RM_MSG));
			String create_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.Receipt.CREATE_TIME));
			String update_datetime  = cursor.getString(cursor.getColumnIndex(DatabaseSchema.Receipt.UPDATE_TIME));
			String large = cursor.getString(cursor.getColumnIndex(DatabaseSchema.Receipt.RM_LARGE));
			String justify = cursor.getString(cursor.getColumnIndex(DatabaseSchema.Receipt.RM_JUSTIFY));
			
			if (large == null) large = "N";
			if (justify == null) justify = "L";
			//large(size) -- 1.large(L) 2.normal(N)
			//justify -- 1.Left(L) 2.Center(C) 3.Right(R) 
			obj.add(new Receipt(id, type, msg, large, justify, create_datetime, update_datetime));
		}
		cursor.close();
		return obj;
	}
	
	public boolean insertReceipt(Receipt obj){
		boolean check = true;
		String sql,type,msg,large,justify;
		int id = obj.getId();
		type = obj.getType();
		msg = obj.getMsg();
		large = obj.getLarge();
		justify = obj.getJustify();
		
		sql = "INSERT INTO " + DatabaseSchema.Receipt.SCHEMA_NAME + " values";
		sql += "("+ id +",'"+ type +"','"+ msg +"',strftime('%Y%m%d%H%M%S','now')," +
				"strftime('%Y%m%d%H%M%S','now'),'"+ large +"','"+ justify+"')";
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean updateReceipt(Receipt obj){
		boolean check = true;
		String sql,type,msg,large,justify;
		int id = obj.getId();
		type = obj.getType();
		msg = obj.getMsg();
		large = obj.getLarge();
		justify = obj.getJustify();
		
		sql = "UPDATE " + DatabaseSchema.Receipt.SCHEMA_NAME + " set ";
		sql += "rm_type = '"+ type +"',rm_msg = '"+ msg +"',update_datetime = strftime('%Y%m%d%H%M%S','now')," +
				"rm_large = '"+ large +"',rm_justify = '"+ justify +"' where rm_id = "+ id;
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public boolean deleteReceipt(int id){
		boolean check = true;
		String sql = "delete from " + DatabaseSchema.Receipt.SCHEMA_NAME + " where rm_id = " + id;
		errorString = DBHelper.execSQL(sql);
		if (errorString.length() > 0) {
			check = false;
		}
		return check;
	}
	
	public int getMaxReceiptNo(){
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		String sql = "select Max(RM_ID) from " + DatabaseSchema.Receipt.SCHEMA_NAME;
		int i = 1;
		Cursor cursor = db.rawQuery(sql, null);
		
		if(cursor == null){
			MessageBox.show(mContext,DBError.error);
			DBError.error = "";
			return -1;
		}
		if (cursor.moveToNext()) {
			i = cursor.getInt(0) +1;
		}
		cursor.close();
		return i;
	}
}
